package com.food.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.food.model.Product;
import com.food.dao.service.ProductService;

/**
 * Servlet implementation class DisplayProduct
 */
@WebServlet("/shopping/DisplayProduct")
public class DisplayProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;

	int pageNo = 1;


	public DisplayProduct() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");

		// PrintWriter out = response.getWriter();
		//
		// for(Product xx:coll) {
		// out.print(xx);
		// }

		// Product one = service.findOneProduct(bean);
		// request.setAttribute("products_one", one);

		/* 取得參數 */
		String pname = request.getParameter("pname");
		String pcountry = request.getParameter("pcountry");
		String pcooker = request.getParameter("pcooker");
		
		String pageNumberStr = "0";
		if (request.getParameter("pageNumber")!=null) {
			pageNumberStr = request.getParameter("pageNumber");
		}

		int pageNumber = 0;
		
		request.removeAttribute("now");
		request.removeAttribute("pre");
		request.removeAttribute("next");
		request.removeAttribute("last");

		String action = request.getParameter("action");
		String type = request.getParameter("type");
		/* 印出結果 */
		System.out.println("pname:" + pname);
		System.out.println("pcountry:" + pcountry);
		System.out.println("pcooker:" + pcooker);

		System.out.println("action:" + action);

		/* 叫用服務 */
		ProductService prodsv = new ProductService();
		
			if ("MANAGER".equals(type)) {
					
			
			/* 建立 List<Product> */
			List<Product> Beanlist = new ArrayList<Product>();

			/* 從服務取得 物件集合 */
			// Beanlist = prodsv.findAllProduct();

			// 分頁6筆

			pageNumber = Integer.parseInt(pageNumberStr);
			request.setAttribute("now", pageNumber);
			request.setAttribute("first", "/shopping/DisplayProduct?action=serch_all&pageNumber=0&type=MANAGER");
			request.setAttribute("pre", "/shopping/DisplayProduct?action=serch_all&type=MANAGER&pageNumber=" + (pageNumber - 1));
			request.setAttribute("next","/shopping/DisplayProduct?action=serch_all&type=MANAGER&pageNumber="+( pageNumber + 1));
			
			
			//每頁6筆，頁數
			Beanlist = prodsv.findAllProductPages(6, pageNumber);
			
			

			
			List<Product> fAll = prodsv.findAllProduct();
			
			int last = fAll.size()/6;
			request.setAttribute("toEnd", "/shopping/DisplayProduct?action=serch_all&type=MANAGER&pageNumber="+last);
			request.setAttribute("total", last);
			
			if(last==pageNumber){
			request.setAttribute("last", last);
			}

			// country
			Set<String> fCountry = new LinkedHashSet<String>();

			for (Product gCountry : fAll) {
				fCountry.add(gCountry.getPcountry());

			}

			for (String tt : fCountry) {
				System.out.println(tt);
			}

			// cooker
			Set<String> fCooker = new LinkedHashSet<String>();

			for (Product gCooker : fAll) {
				fCooker.add(gCooker.getPcooker());

			}

			for (String yy : fCooker) {
				System.out.println(yy);
			}

			/* 設定傳出物件名稱:products_Det 內容:xxx */
			request.setAttribute("products_location", fCountry);

			request.setAttribute("products_cooker", fCooker);

			request.setAttribute("products_Det", Beanlist);
			RequestDispatcher rd = request
					.getRequestDispatcher("/manager/manageproduct.jsp");
			rd.forward(request, response);
			return;
		}

		/* 當 action=serch_all */
		if ("serch_all".equals(action)) {
					
			
			/* 建立 List<Product> */
			List<Product> Beanlist = new ArrayList<Product>();

			/* 從服務取得 物件集合 */
			// Beanlist = prodsv.findAllProduct();

			// 分頁6筆

			pageNumber = Integer.parseInt(pageNumberStr);
			request.setAttribute("now", pageNumber);
			request.setAttribute("first", "/shopping/DisplayProduct?action=serch_all&pageNumber=0");
			request.setAttribute("pre", "/shopping/DisplayProduct?action=serch_all&pageNumber=" + (pageNumber - 1));
			request.setAttribute("next","/shopping/DisplayProduct?action=serch_all&pageNumber="+( pageNumber + 1));
			
			
			//每頁6筆，頁數
			Beanlist = prodsv.findAllProductPages(6, pageNumber);
			
			

			
			List<Product> fAll = prodsv.findAllProduct();
			
			int last = fAll.size()/6;
			request.setAttribute("toEnd", "/shopping/DisplayProduct?action=serch_all&pageNumber="+last);
			request.setAttribute("total", last);
			
			if(last==pageNumber){
			request.setAttribute("last", last);
			}

			// country
			Set<String> fCountry = new LinkedHashSet<String>();

			for (Product gCountry : fAll) {
				fCountry.add(gCountry.getPcountry());

			}

			for (String tt : fCountry) {
				System.out.println(tt);
			}

			// cooker
			Set<String> fCooker = new LinkedHashSet<String>();

			for (Product gCooker : fAll) {
				fCooker.add(gCooker.getPcooker());

			}

			for (String yy : fCooker) {
				System.out.println(yy);
			}

			/* 設定傳出物件名稱:products_Det 內容:xxx */
			request.setAttribute("products_location", fCountry);

			request.setAttribute("products_cooker", fCooker);

			request.setAttribute("products_Det", Beanlist);
			RequestDispatcher rd = request
					.getRequestDispatcher("/shopping/shoppingFirst.jsp");
			rd.forward(request, response);
			return;
		}

		/* 當 action=serch_customer */
		if ("search_customer".equals(action)) {
			
			/* 建立 List<Product> */
			List<Product> Beanlist1 = new ArrayList<Product>();

			/* 從服務取得 物件集合 */
			// Beanlist = prodsv.findAllProduct();

			// 分頁6筆

			pageNumber = Integer.parseInt(pageNumberStr);
			request.setAttribute("now", pageNumber);
			request.setAttribute("first", "/shopping/DisplayProduct?action=search_customer&pname="+pname+"&pageNumber=0");
			request.setAttribute("pre", "/shopping/DisplayProduct?action=search_customer&pname="+pname+"&pageNumber="+(pageNumber - 1));
			request.setAttribute("next","/shopping/DisplayProduct?action=search_customer&pname="+pname+"&pageNumber="+( pageNumber + 1));
			
			
			//每頁6筆，頁數			
			
			Beanlist1 = prodsv.find_Search_Pages(6, pageNumber, pname);
			
			int size = Beanlist1.size();
			
			request.setAttribute("size", size);
			
			int last = Beanlist1.size()/6;
			request.setAttribute("toEnd", "/shopping/DisplayProduct?action=search_customer&pname="+pname+"&pageNumber="+last);
			request.setAttribute("total", last);
			
			if(last==pageNumber){
			request.setAttribute("last", last);
			}
			

			/* 從服務取得 物件 */
//			List<Product> findPname = prodsv.findProductByName(pname);
//
//			for (Product tt : findPname) {
//				System.out.print(tt);
//			}
//		

			/* 設定傳出物件名稱:products_Det 內容:findPid */
			request.setAttribute("products_Det", Beanlist1);

			/* 轉移 */
			RequestDispatcher rd = request
					.getRequestDispatcher("/shopping/shoppingFirst.jsp");
			rd.forward(request, response);
			return;
		}

		/* 當 action=search_pcountry */
		if ("search_pcountry".equals(action)) {
			
			
			/* 建立 List<Product> */
			List<Product> Beanlist1 = new ArrayList<Product>();

			/* 從服務取得 物件集合 */
			// Beanlist = prodsv.findAllProduct();

			// 分頁6筆

			pageNumber = Integer.parseInt(pageNumberStr);
			request.setAttribute("now", pageNumber);
			request.setAttribute("first", "/shopping/DisplayProduct?action=search_pcountry&pcountry="+pcountry+"&pageNumber=0");
			request.setAttribute("pre", "/shopping/DisplayProduct?action=search_pcountry&pcountry="+pcountry+"&pageNumber="+(pageNumber - 1));
			request.setAttribute("next","/shopping/DisplayProduct?action=search_pcountry&pcountry="+pcountry+"&pageNumber="+( pageNumber + 1));
			
			
			//每頁6筆，頁數			
			
			Beanlist1 = prodsv.find_Search_Pages(6, pageNumber, pcountry);
			
			int last = Beanlist1.size()/6;
			request.setAttribute("toEnd", "/shopping/DisplayProduct?action=search_pcountry&pcountry="+pcountry+"&pageNumber="+last);
			request.setAttribute("total", last);
			
			if(last==pageNumber){
			request.setAttribute("last", last);
			}
			
			

			/* 從服務取得 物件 */
			
			//List<Product> findcountry = prodsv.findProductByCountry(pcountry);

			//System.out.println(findcountry);

			/* 建立 List<Product> */

			/* 物件丟進集合 */

			/* 設定傳出物件名稱:products_Det 內容:findPcountry */
			request.setAttribute("products_Det", Beanlist1);

			/* 轉移 */
			RequestDispatcher rd = request
					.getRequestDispatcher("/shopping/shoppingFirst.jsp");
			rd.forward(request, response);
			return;
		}

		/* 當 action=search_pcooker */
		if ("search_pcooker".equals(action)) {

			
			/* 建立 List<Product> */
			List<Product> Beanlist2 = new ArrayList<Product>();

			/* 從服務取得 物件集合 */
			// Beanlist = prodsv.findAllProduct();

			// 分頁6筆

			pageNumber = Integer.parseInt(pageNumberStr);
			request.setAttribute("now", pageNumber);
			request.setAttribute("first", "/shopping/DisplayProduct?action=search_pcooker&pcooker="+pcooker+"&pageNumber=0");
			request.setAttribute("pre", "/shopping/DisplayProduct?action=search_pcooker&pcooker="+pcooker+"&pageNumber="+(pageNumber - 1));
			request.setAttribute("next","/shopping/DisplayProduct?action=search_pcooker&pcooker="+pcooker+"&pageNumber="+( pageNumber + 1));
			
			
			//每頁6筆，頁數			
			
			Beanlist2 = prodsv.find_Search_Pages(6, pageNumber, pcooker);
			
			int last = Beanlist2.size()/6;
			request.setAttribute("toEnd", "/shopping/DisplayProduct?action=search_pcooker&pcooker="+pcooker+"&pageNumber="+last);
			request.setAttribute("total", last);
			
			if(last==pageNumber){
			request.setAttribute("last", last);
			}
			

			
			/* 從服務取得 物件 */

			//List<Product> findPcooker = prodsv.findProductByCooker(pcooker);

			//System.out.println(findPcooker);

			/* 建立 List<Product> */

			/* 物件丟進集合 */

			/* 設定傳出物件名稱:products_Det 內容:findPcooker */
			request.setAttribute("products_Det", Beanlist2);

			/* 轉移 */
			RequestDispatcher rd = request
					.getRequestDispatcher("/shopping/shoppingFirst.jsp");
			rd.forward(request, response);
			return;
		}
		
		String path = request.getContextPath();
		response.sendRedirect(path+"/shopping/DisplayProduct?action=serch_all&pageNumber=0");

	}
}
